package com.example.teamtribe.main.club.manage.finace;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.database.Bill;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import java.util.List;

public class FinanceAdapter extends RecyclerView.Adapter<FinanceAdapter.ViewHolder> {

    private List<Bill> bills;

    public FinanceAdapter(List<Bill> bills) {
        this.bills = bills;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bill, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Bill bill = bills.get(position);
        holder.tvDate.setText(bill.getDate());
        User user = Utility.getT(bill.getUserId(), User.class);
        if (user != null) {
            holder.tvUser.setText("经办人：" + user.getName());
        }
        holder.tvName.setText(bill.getName());
        holder.tvUse.setText(bill.getUse());
        holder.tvIn.setText("收入：" + bill.getIn());
        holder.tvOut.setText("支出：" + bill.getOut());
        if (position == 0)
            holder.tvTop.setVisibility(View.INVISIBLE);
    }

    @Override
    public int getItemCount() {
        return bills.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTop;
        TextView tvDate;
        TextView tvUser;
        TextView tvName;
        TextView tvUse;
        TextView tvOut;
        TextView tvIn;

        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvUser = itemView.findViewById(R.id.tv_user);
            tvName = itemView.findViewById(R.id.tv_name);
            tvUse = itemView.findViewById(R.id.tv_use);
            tvOut = itemView.findViewById(R.id.tv_out);
            tvIn = itemView.findViewById(R.id.tv_in);
            tvTop = itemView.findViewById(R.id.tvTopLine);
        }
    }
}
